﻿package net.jmp0.toast.entities.obstacles 
{
	import net.jmp0.toast.entities.bases.GameElement;
	import net.jmp0.toast.entities.obstacles.Obstacle;
	
	/**
	 * A Toaster :D
	 * @author Thodd
	 */
	public class Toaster extends Obstacle
	{
		[Embed(source = '../../res/toaster_left.png')] private var imgToasterLeft:Class;
		[Embed(source = '../../res/toaster_up.png')] private var imgToasterUp:Class;
		[Embed(source = '../../res/toaster_down.png')] private var imgToasterDown:Class;
		[Embed(source = '../../res/toaster_right.png')] private var imgToasterRight:Class;
		//sprite const
				
		
		//Directionvariables
		//These indicate the directions in which the player will be redirected
		private var xDir:int = 0;
		private var yDir:int = 0;
		
		public function Toaster(x:int, y:int, xDir:int, yDir:int)
		{
			super(x, y);
			this.xDir = xDir;
			this.yDir = yDir;
			
			var imgSprite:Class = chooseSprite();
			
			//Standard setup stuff
			this.sprite = FP.getSprite(imgSprite, 25, 25, false, false, 4, 4, true);
			this.randomizeAnimation();
			
		}
		
		private function chooseSprite():Class {
			
			var spr:Class = imgToasterDown;
			
			if (xDir == -1 && yDir == 0) {
				spr = imgToasterLeft;
				
			}else if (xDir == 0 && yDir == -1) {
				spr =  imgToasterUp;
				
			}else if (xDir == 0 && yDir == 1) {
				spr =  imgToasterDown;
				
			}else if (xDir == 1 && yDir == 0) {
				spr =  imgToasterRight;
				
			}
				
			return spr;
			
		}
		
		public function getXDir():int {
			return this.xDir;
		}
				
		public function getYDir():int {
			return this.yDir;
		}
	
		override public function performCollision(colPartner:GameElement):void {
			colPartner.xSpeed = this.getXDir() * colPartner.movingSpeed;
			colPartner.ySpeed = this.getYDir() * colPartner.movingSpeed;
			
			if (colPartner.xSpeed != 0 || colPartner.ySpeed != 0) {
				colPartner.x = this.x;
				colPartner.y = this.y;
				colPartner.lastCollision = this; //colPartner is important! It prevents double collision performing
			}
		}
		
	}

}